package Algorithm.ArrayExercise;

import java.util.ArrayList;
import java.util.List;

/**
 * 题目：给定两个升序数组，合并成一个降序数组
 */
public class ArrayTest01 {
    public static void main(String[] args) {
        int[] arr1={1,6,9,35,96,157,598,1235,6652};
        int[] arr2={2,6,58,423,985,7844};

        List<Integer> list=function(arr1,arr2);
        System.out.println(list.size());
        System.out.print(list.toString());
    }
    public static List function(int[] arr1,int[] arr2){
        List<Integer> list=new ArrayList<>();
        //这里特别注意数组越界问题
        int i=arr1.length-1;
        int j=arr2.length-1;
        while(i>0&&j>0){
            if(arr1[i]>arr2[j]){
                list.add(arr1[i--]);
            }
            if(arr1[i]<=arr2[j]){
                list.add(arr2[j--]);
            }
        }
        while (i>0){
            list.add(arr1[i--]);
        }
        while (j>0){
            list.add(arr2[j--]);
        }
        return list;
    }

}
